package com.hy.solr.service;

import com.hy.solr.config.ExeclUtil;
import com.hy.solr.entity.ModelEntity;
import fr.opensagres.xdocreport.document.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.solr.core.SolrTemplate;
import org.springframework.data.solr.core.query.Query;
import org.springframework.data.solr.core.query.SimpleQuery;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static com.hy.solr.config.StaticData.SOLRCORE;

@Service
public class ModelService {
    @Autowired
    private SolrTemplate solrTemplate;
    @Autowired
    private SolrClient solrClient;

    public boolean insert(ModelEntity modelEntity){
        //需要返回值，要根据返回值进行一些别的操作的，可以用UpdateResponse接收返回值
        UpdateResponse updateResponse=solrTemplate.saveBean(SOLRCORE,modelEntity);
        solrTemplate.commit(SOLRCORE);
        return updateResponse.getStatus()==0;
    }
    public boolean update(ModelEntity modelEntity){
        UpdateResponse updateResponse=solrTemplate.saveBean(SOLRCORE,modelEntity);
        solrTemplate.commit(SOLRCORE);
        return updateResponse.getStatus()==0;
    }
    public boolean delete(String id){
        UpdateResponse updateResponse=solrTemplate.deleteByIds(SOLRCORE,id);
        solrTemplate.commit(SOLRCORE);
        return updateResponse.getStatus()==0;
    }
    public Page<ModelEntity> select(){
        String solrSql="*:*";
        Query query=new SimpleQuery(solrSql);
        //solr 分页索引从零开始
        PageRequest pageRequest=PageRequest.of(0, 100);
        //分页不只是可以设置分页，还可以设置排序字段等等
//        PageRequest pageRequest=PageRequest.of(0, 100, Sort.by(Sort.Order.asc("age"),Sort.Order.asc("age"),Sort.Order.asc("age")));
        query.setPageRequest(pageRequest);
        return solrTemplate.query(SOLRCORE,query,ModelEntity.class);
    }
    public XSSFWorkbook poiout(Integer page,Integer pagesize) throws IOException, SolrServerException {
        String solrSql="zzzz9998:86330502*";
        solrSql+=" AND byux0054:(1 OR 5) ";
        SolrQuery solrQuery=new SolrQuery(solrSql);
        solrQuery.setStart(page);
        solrQuery.setRows(pagesize);
        SolrDocumentList datalist= solrClient.query("hvit-test",solrQuery).getResults();
        List<String> colums = Arrays.asList("byux0037","byux0056","byux0001","byux0054");
        return ExeclUtil.createXMlModel(colums,datalist.stream().collect(Collectors.toList()), "Sheet1");
    }

    /**
     * 根据传参{page, pageSize} 获得PageRequest{@link PageRequest}
     *
     * @return
     */
    public PageRequest convertToPage(Integer page,Integer pageSize) {return PageRequest.of(page, pageSize);}

    public static void main(String[] args) {
        File file=new File("E:\\浙江海予工作文档\\7-30\\front\\build");
//        file.fil
    }
}
